Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports System.Data

Partial Class AdobePopup
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim sDT As New Data.DataTable
        Dim sConn As New Data.SqlClient.SqlConnection
        Dim sCom As New Data.SqlClient.SqlCommand
        Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)
        Dim sDS As New Data.DataSet

        sCom.CommandType = Data.CommandType.Text
        sCom.CommandText = Session("sqlReport")

        Try
            'sConn.ConnectionString = "User ID=blue;Password=2blue;Initial Catalog=cims;Data Source=OEMKID"
            sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")

            sCom.Connection = sConn
            sConn.Open()
            sDApt.Fill(sDS)

            sDS.Tables(0).TableName = Session("sqlUseTable").ToString

            Dim objReport As New ReportDocument

            '      MsgBox(Server.MapPath(Session("sqlUseReport").ToString))
            objReport.Load(Server.MapPath(Session("sqlUseReport").ToString) & ".rpt")

            'objReport.PrintOptions.PaperOrientation = PaperOrientation.Landscape
            objReport.SetDataSource(sDS)

            Dim expReport As ExportOptions
            expReport = objReport.ExportOptions
            expReport.FormatOptions = New PdfRtfWordFormatOptions 'PDF format option
            expReport.ExportFormatType = ExportFormatType.PortableDocFormat 'Set the format type of document
            Dim req As ExportRequestContext = New ExportRequestContext
            req.ExportInfo = expReport

            'Sets the report to HTML stream
            Dim st As System.IO.Stream
            st = objReport.FormatEngine.ExportToStream(req)

            objReport = Nothing 'Dispose the report

            'Stream content
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"

            Dim b(st.Length) As Byte
            st.Read(b, 0, st.Length) 'Read Stream
            Response.BinaryWrite(b) 'Write Stream
            st.Close() 'Close Stream
            st = Nothing 'Dispose

            Response.End()

        Catch ex As Exception

        Finally
            sConn.Close()
        End Try

    End Sub
End Class